﻿using System;
using System.Configuration;
using System.Web;
using Shuba.Model;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace Shuba.Web.master
{
    public partial class branch_plan_add : basePage
    {
        string msg = "", ActType;
        Int64 TotalNoaBranch = 0, TotalAmountBranch = 0, TotalNoaBranchReal = 0, TotalAmountBranchReal = 0;
        bool IsView, IsAdd, IsEdit;

        protected void Page_Load(object sender, EventArgs e)
        {
            //this.Form.DefaultButton = btnSubmit.UniqueID;

            if (!Page.IsPostBack)
            {
                string acl = GetUserAccess("TARGET_BRANCH");
                
                IsView = IsUserView(acl);
                IsAdd = IsUserAdd(acl);
                IsEdit = IsUserEdit(acl);

                LabelTitle.Text = GetPageTitle("TARGET_BRANCH");

                string SelectedYear = Request.QueryString["yr"].ToString().Trim();
                string SelectedRegion = Request.QueryString["reg"].ToString().Trim();
                string SelectedCluster = Request.QueryString["clus"].ToString().Trim();
                string SelectedUnit = Request.QueryString["id"].ToString().Trim();
                ActType = Request.QueryString["a"].ToString().Trim();

                HidUnitID.Value = SelectedUnit;

                Shuba.Business.Regional _Region = new Shuba.Business.Regional();
                Shuba.Business.Cluster _Cluster = new Shuba.Business.Cluster();
                Shuba.Business.Branch _Branch = new Shuba.Business.Branch();

                //Shuba.Model.RegionalInfo RI;

                lblYear.Text = SelectedYear;
                lblRegion.Text = _Region.Get(SelectedRegion).RegionalName;
                lblCluster.Text = _Cluster.Get(SelectedCluster).ClusterName;
                lblUnit.Text = _Branch.Get(SelectedUnit).BranchName;

                if (!IsView)
                {
                    msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString(); 
                    AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
                }

                Shuba.Business.BranchTarget _BranchTarget = new Shuba.Business.BranchTarget();
                List<BranchTargetInfo> list;
                list = _BranchTarget.GetList(SelectedUnit, SelectedYear);
                gvList.DataSource = list;
                gvList.DataBind();

                //Target Cluster
                Shuba.Business.ClusterTarget _ClusterTarget = new Shuba.Business.ClusterTarget();
                Shuba.Model.ClusterTargetInfo bi;
                bi = _ClusterTarget.Get(SelectedCluster, SelectedYear);

                lblAmount.Text = bi.TargetAmount.ToString("N0");
                lblNoa.Text = bi.Noa.ToString("N0");
                lblAmountReal.Text = bi.TargetAmountReal.ToString("N0");
                lblNoaReal.Text = bi.NoaReal.ToString("N0");

                //Total yang sudah di assigned di Unit bersangkutan
                lblAmountBranch.Text = TotalAmountBranch.ToString("N0");
                lblNoaBranch.Text = TotalNoaBranch.ToString("N0");
                lblAmountBranchReal.Text = TotalAmountBranchReal.ToString("N0");
                lblNoaBranchReal.Text = TotalNoaBranchReal.ToString("N0");

                //Total yang sudah di assigned di cluster bersangkutan
                bi = _ClusterTarget.GetClusterTarget(SelectedCluster, SelectedYear);
                lblAssignedAmount.Text = bi.TargetAmount.ToString("N0"); ;
                lblAssignedNoa.Text = bi.Noa.ToString("N0");
                lblAssignedAmountReal.Text = bi.TargetAmountReal.ToString("N0");
                lblAssignedNoaReal.Text = bi.NoaReal.ToString("N0");

                if (ActType == "vw" || !IsAdd || !IsEdit) btnSubmit.Enabled = false; else btnSubmit.Enabled = true;
            }
        }
        
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string ActType = "INSERT";
            bool IsValidEntry = false;
            
            Int64 TargetAmountReal = 0;
            Int32 TargetNoaReal = 0;
            
            Int64 TotalAmount = Convert.ToInt64(lblAmount.Text.Replace(",",""));
            Int64 TotalNoa = Convert.ToInt64(lblNoa.Text.Replace(",", ""));

            Shuba.Business.ClusterTarget _clusterTarget = new Shuba.Business.ClusterTarget();
            Shuba.Model.ClusterTargetInfo ctbi;
            ctbi = _clusterTarget.GetClusterTargetTotal(Request.QueryString["clus"].ToString().Trim(), Request.QueryString["id"].ToString().Trim(), Request.QueryString["yr"].ToString().Trim());

            Int64 TargetAmount = ctbi.TargetAmount;
            Int32 TargetNoa = ctbi.Noa;

            foreach (GridViewRow row in gvList.Rows)
            {
                TextBox txtTargetAmount = (TextBox)row.FindControl("txtTargetAmount");
                TextBox txtTargetNoa = (TextBox)row.FindControl("txtNoa");

                if (txtTargetAmount.Text != "" && txtTargetNoa.Text != "")
                {
                    TargetAmount += Convert.ToInt64(txtTargetAmount.Text);
                    TargetNoa += Convert.ToInt32(txtTargetNoa.Text);
                }
            }

            if ((TargetAmount <= TotalAmount) && (TargetNoa <= TotalNoa)) IsValidEntry = true;

            if (IsValidEntry)
            {
                Shuba.Business.BranchTarget _BranchTarget = new Shuba.Business.BranchTarget();
                Shuba.Model.BranchTargetInfo bi = new Shuba.Model.BranchTargetInfo();

                foreach (GridViewRow row in gvList.Rows)
                {
                    TextBox txtTargetAmount = (TextBox)row.FindControl("txtTargetAmount");
                    TextBox txtTargetNoa = (TextBox)row.FindControl("txtNoa");
                    TextBox txtTargetAmountReal = (TextBox)row.FindControl("txtTargetAmountReal");
                    TextBox txtTargetNoaReal = (TextBox)row.FindControl("txtNoaReal");

                    RangeValidator AmountValidator = (RangeValidator)row.FindControl("RangeValidatorAmount");
                    RangeValidator NoaValidator = (RangeValidator)row.FindControl("RangeValidatorNoa");
                    RangeValidator AmountValidatorReal = (RangeValidator)row.FindControl("RangeValidatorAmountReal");
                    RangeValidator NoaValidatorReal = (RangeValidator)row.FindControl("RangeValidatorNoaReal");

                    if (txtTargetAmount.Text != "") TargetAmount = Convert.ToInt64(txtTargetAmount.Text); else TargetAmount = 0;
                    if (txtTargetNoa.Text != "") TargetNoa = Convert.ToInt32(txtTargetNoa.Text); else TargetNoa = 0;

                    if (txtTargetAmountReal.Text != "") TargetAmountReal = Convert.ToInt64(txtTargetAmountReal.Text); else TargetAmountReal = 0;
                    if (txtTargetNoaReal.Text != "") TargetNoaReal = Convert.ToInt32(txtTargetNoaReal.Text); else TargetNoaReal = 0;

                    //string id = Convert.ToString(gvList.DataKeys[row.RowIndex].Value.ToString());
                    //if (TargetAmount > 0 || TargetNoa > 0 || TargetAmountReal > 0 || TargetNoaReal > 0)
                    //{
                        bi.BranchID = HidUnitID.Value;
                        bi.TargetYear = lblYear.Text;
                        bi.TARGET_MONTH = Convert.ToString(gvList.DataKeys[row.RowIndex].Value.ToString()); //Grid Month
                        bi.TargetAmount = TargetAmount;
                        bi.Noa = TargetNoa;
                        bi.TargetAmountReal = TargetAmountReal;
                        bi.NoaReal = TargetNoaReal;
                        bi.EntryBy = HttpContext.Current.User.Identity.Name;
                        bi.EntryDate = ProcessingDate();
                        bi.UpdateBy = HttpContext.Current.User.Identity.Name;
                        bi.UpdateDate = ProcessingDate();
                        bi.ActType = ActType;

                        _BranchTarget.Save(bi);

                        AmountValidator.Visible = false;
                        NoaValidator.Visible = false;
                        AmountValidatorReal.Visible = false;
                        NoaValidatorReal.Visible = false;

                        AddLog(ActType, ActType + " BRANCH TARGET PLAN");
                    //}
                }

                msg = this.GetGlobalResourceObject("Messages", "SaveAlert").ToString();
                AlertAndRedirect(msg, "branch_plan_list.aspx");
            }
            else
            {
                Alert("Total NOA or Total Amount can not greater than Cluster Target Plan, please confirm your entry");
            }
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox txtNoa = (TextBox)e.Row.FindControl("txtNoa");
                TextBox txtTargetAmount = (TextBox)e.Row.FindControl("txtTargetAmount");
                TextBox txtNoaReal = (TextBox)e.Row.FindControl("txtNoaReal");
                TextBox txtTargetAmountReal = (TextBox)e.Row.FindControl("txtTargetAmountReal");

                if (txtNoa.Text != "") TotalNoaBranch += Convert.ToInt64(txtNoa.Text);
                if (txtTargetAmount.Text != "") TotalAmountBranch += Convert.ToInt64(txtTargetAmount.Text);

                if (txtNoaReal.Text != "") TotalNoaBranchReal += Convert.ToInt64(txtNoaReal.Text);
                if (txtTargetAmountReal.Text != "") TotalAmountBranchReal += Convert.ToInt64(txtTargetAmountReal.Text);

                if (ActType == "vw" || !IsAdd || !IsEdit)
                {
                    txtTargetAmount.Enabled = false;
                    txtNoa.Enabled = false;
                    txtTargetAmountReal.Enabled = false;
                    txtNoaReal.Enabled = false;
                }
                else
                {
                    txtTargetAmount.Enabled = true;
                    txtNoa.Enabled = true;
                    txtTargetAmountReal.Enabled = true;
                    txtNoaReal.Enabled = true;
               }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("branch_plan_list.aspx");
        }
    }
}
